package audit

import (
	"counter-help/admin/server/model/audit"
	"counter-help/admin/server/model/common/response"
	"github.com/gin-gonic/gin"
)

type AfterSalesApi struct{}

// 创建售后
func (a *AfterSalesApi) CreateAfterSales(c *gin.Context) {
	var afterSales audit.AfterSales
	if err := c.ShouldBindJSON(&afterSales); err != nil {
		response.FailWithMessage("参数解析失败", c)
		return
	}

	if err := afterSalesService.CreateAfterSales(afterSales); err != nil {
		response.FailWithMessage("创建售后失败", c)
		return
	}
	response.OkWithMessage("售后创建成功", c)
}

// CancelAfterSales 撤销售后
func (a *AfterSalesApi) CancelAfterSales(c *gin.Context) {
	afterSalesID := c.Param("id")
	if err := afterSalesService.CancelAfterSales(afterSalesID); err != nil {
		response.FailWithMessage("撤销售后失败", c)
		return
	}
	response.OkWithMessage("售后已撤销", c)
}

// 审核通过
func (a *AfterSalesApi) ApproveAfterSales(c *gin.Context) {
	afterSalesID := c.Param("id")
	if err := afterSalesService.ApproveAfterSales(afterSalesID); err != nil {
		response.FailWithMessage("审核失败", c)
		return
	}
	response.OkWithMessage("售后审核通过", c)
}

// 审核不通过
func (a *AfterSalesApi) RejectAfterSales(c *gin.Context) {
	afterSalesID := c.Param("id")
	var rejectData struct {
		Reason         string `json:"reason"`
		DetailedReason string `json:"detailedReason"` // 新增的详细原因
	}
	if err := c.ShouldBindJSON(&rejectData); err != nil {
		response.FailWithMessage("参数解析失败", c)
		return
	}

	if err := afterSalesService.RejectAfterSales(afterSalesID, rejectData.Reason, rejectData.DetailedReason); err != nil {
		response.FailWithMessage("审核失败", c)
		return
	}
	response.OkWithMessage("售后审核不通过", c)
}

// 对方同意
func (a *AfterSalesApi) AgreeAfterSales(c *gin.Context) {
	afterSalesID := c.Param("id")
	if err := afterSalesService.AgreeAfterSales(afterSalesID); err != nil {
		response.FailWithMessage("操作失败", c)
		return
	}
	response.OkWithMessage("对方已同意", c)
}

// 退款操作
func (a *AfterSalesApi) RefundAfterSales(c *gin.Context) {
	afterSalesID := c.Param("id")
	var refundData struct {
		RefundAmount float64 `json:"refundAmount"`
	}
	if err := c.ShouldBindJSON(&refundData); err != nil {
		response.FailWithMessage("参数解析失败", c)
		return
	}

	if err := afterSalesService.RefundAfterSales(afterSalesID, refundData.RefundAmount); err != nil {
		response.FailWithMessage("退款操作失败", c)
		return
	}
	response.OkWithMessage("退款操作成功", c)
}
